package com.ssyc.gsk_tk.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.view.flowlayout.FlowLayout;
import com.ssyc.common.view.flowlayout.TagAdapter;
import com.ssyc.common.view.flowlayout.TagFlowLayout;
import com.ssyc.common.view.vp.SuperViewPager;
import com.ssyc.gsk_tk.R;
import com.ssyc.gsk_tk.bean.AnswerInfo;
import com.ssyc.gsk_tk.bean.GapFillingInfo;
import com.ssyc.gsk_tk.bean.OverQuestion;
import com.ssyc.gsk_tk.bean.QuestionInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 选词填句子
 * Created by Administrator on 2018/9/3 0003.
 */

public class TkGapFillingFragment extends LazyBaseFragment implements ViewPager.OnPageChangeListener{


    //页面对象
    private QuestionInfo.ListBean.ItemBean info;

    //页面的json数据
    private String jsonData;

    private SuperViewPager vp;

    //选词填句子
    private GapFillingInfo articleInfo;

    //vp的页面指示器
    private TextView tvNum;

    //填充ViewPager的Framgent集合
    private List<Fragment> mFragments;

    //vp的适配器
    private VpCommonAdapter adapter;

    //展示提供选择单词的流式布局
    private TagFlowLayout flowLayout;

    //流式布局适配器
    private TagAdapter tagAdapter;

    //已完成的页面对象
    private OverQuestion.ListBean.ItemBean overinfo;

    //所有小题的解析的数组
    private String[] analysis;
    //所有小题自己选择的答案的数组
    private String[] chooseAnswers;

    //所有小题选项的正确率
    private List<OverQuestion.ListBean.ItemBean.ProbabilityBean> probability;


    public static final TkGapFillingFragment newInstance(QuestionInfo.ListBean.ItemBean item, OverQuestion.ListBean.ItemBean overData) {
        TkGapFillingFragment teacherGoldFragment = new TkGapFillingFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", item);
        bundle.putSerializable("overdata", overData);
        teacherGoldFragment.setArguments(bundle);
        return teacherGoldFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tk_gap_filling;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //显示内容
        showContent();

        //获取页面数据
        initIntent();

        //初始化视图
        initView(view);

        //设置顶部流式布局展示单词的数据
        initFlowLayoutData();

        //初始化页面数据
        initData();
    }


    /**
     * 获取页面数据
     */
    private void initIntent() {
        info = (QuestionInfo.ListBean.ItemBean) getArguments().getSerializable("data");
        overinfo = (OverQuestion.ListBean.ItemBean) getArguments().getSerializable("overdata");


        if (info != null) {
            //获取页面的JSON数据
            jsonData = info.getContent();

            Log.e("test","选词填句子："+jsonData);

            if (!TextUtils.isEmpty(jsonData)) {
                articleInfo = GsonUtil.jsonToBean(jsonData, GapFillingInfo.class);
            }
        }

        //已完成
        if (overinfo != null) {
            //获取解析和自己填写的答案的集合
            //获取所有题目的解析集合使用￠切割
            analysis = overinfo.getAnalysis().split("￠");

            //获取所有的自己作答的选项集合
            chooseAnswers = overinfo.getOption_content().split("￠");

            //获取页面的JSON数据
            jsonData = overinfo.getContent();

            Log.i("test","选词填句子json:"+GsonUtil.objectToJson(overinfo));

            if (!TextUtils.isEmpty(jsonData)) {
                articleInfo = GsonUtil.jsonToBean(jsonData, GapFillingInfo.class);
            }
            probability = overinfo.getProbability();
        }
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    private void initView(View view) {
        vp = view.findViewById(R.id.vp);
        vp.addOnPageChangeListener(this);
        tvNum = view.findViewById(R.id.tv_num);
        flowLayout = view.findViewById(R.id.flowlayout);
    }

    /**
     * 设置流式布局的数据
     */
    private void initFlowLayoutData() {
        if (articleInfo == null) {
            Log.i("test", "没有需要展示的单词数据");
            return;
        }

        //获取需要展示的单词
        List<GapFillingInfo.WordsBean> words = articleInfo.getWords();
        if (words == null || words.size() == 0) {
            Log.i("test", "没有需要展示的单词数据");
            return;
        } else {
            List<String> showWords = new ArrayList<>();
            for (int i = 0; i < words.size(); i++) {
                showWords.add(words.get(i).getWord());
            }
            final LayoutInflater mInflater = LayoutInflater.from(getActivity());
            //设置流式布局的数据
            flowLayout.setAdapter(new TagAdapter<String>(showWords) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) mInflater.inflate(R.layout.tk_tv_article,
                            flowLayout, false);
                    tv.setText(s);
                    return tv;
                }
            });
        }
    }

    /**
     * 初始化页面的数据,包含阅读的内容,以及提供选择的
     */
    private void initData() {

        if (articleInfo != null) {

            //获取题目
            List<GapFillingInfo.ListBean> questions = articleInfo.getList();

            //创建底部选择答案的Fragment页面的数据集合
            List<AnswerInfo> answerInfos = new ArrayList<>();

            for (int i = 0; i < questions.size(); i++) {

                AnswerInfo info = new AnswerInfo();

                //设置题目内容
                info.qustionContent = (i + 1) + "." + questions.get(i).getQ();

                //设置正确答案
                info.rightAnswer = articleInfo.getList().get(i).getA();

                if(analysis!=null&&analysis.length!=0) {
                    info.explain = analysis[i];
                }

                if(chooseAnswers!=null&&chooseAnswers.length!=0) {
                    info.chooseAnswer = chooseAnswers[i];
                }
                if (probability != null && probability.size() != 0) {

                    if(TextUtils.isEmpty(probability.get(i).getpro())){
                        info.proba = "100.0%";
                    }else{
                        info.proba = probability.get(i).getpro();
                    }
                }else{
                    info.proba = "100.0%";
                }

                answerInfos.add(info);
            }

            //根据获取的answerInfos来创建填充ViewPager的Fragment
            mFragments = new ArrayList<>();
            if (answerInfos.size() != 0) {
                for (int i = 0; i < answerInfos.size(); i++) {
                    //这里将是否是以完成和未完成的标识传递0代表未完成,1代表已完成
                    if (info != null) {
                        mFragments.add(TkGapFillingAnswerFragment.newInstance(answerInfos.get(i), i, 0));
                    } else if (overinfo != null) {
                        mFragments.add(TkGapFillingAnswerFragment.newInstance(answerInfos.get(i), i, 1));
                    }
                }

                //设置底部的VP的指示器数字
                tvNum.setText("1/" + mFragments.size());

                //设置ViewPager的数据
                adapter = new VpCommonAdapter(getChildFragmentManager(), mFragments);

                vp.setAdapter(adapter);

                vp.setOffscreenPageLimit(mFragments.size());
            }
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvNum.setText((position + 1) + "/" + mFragments.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }



}
